<?php

class Category
{
	private $id = -1; // category id
	private $category = ""; // category name
		
	// Constructor
	function __construct($id = -1, $category = '')
	{
		if($id > 0) $this->id = $id;
		if(!empty($category)) $this->category = $category;
	}
	
	##########################
	# Loads a category from the database and returns Category object
	# Use : $cat = Category::load($cat_id) // loads 1 category identified by its ID, returns a Category object
	# Param : id
	# Returns : object Catgory
	public static function load($id = 'all')
	{
		$db = new Database();
		
		// all categories
		if($id == "all")
		{
			$cs = new CategorySet();
			$query = $db->select("categories");
			while($data = $db->fetch())
			{
				$cs->append(Category::load($data['id']));
			}
			return $cs;
		}
		// one category
		elseif(is_numeric($id))
		{
			
			$ar['id'] = $id;
	
			$db->select("categories",'*',$ar, '1');
			$data = $db->fetch();
	
			return new Category($id, $data['category']);
		}
		// id not numeric (maybe null)
		else
		{
			return false;
		}
		
	}
		
	##########################
	# Creates a NEW category and injects it into the database
	# Use : 1) set the variables with setter methods, 2) call this method
	# Param :
	# Returns : false if category already exists, queryset otherwise
	public function create()
	{
		$data['id'] = '';
		$data['category'] = $this->getCategory();
		
		$db = new Database();
		
		// Hash control : do not create a note if it already exists !
		$db->query("SELECT * FROM categories WHERE category='".$data['category']."'");
		if($db->num_rows() > 0)
			return false;
		
		return $db->insert("categories",$data);
	}
	
	##########################
	# Updates the current note in the database
	# Use : 1) set the variables with setter methods, 2) call this method
	# e.g. $cat->save() // saves all fields in $cat instance
	# Use : 1) set one variable with its setter, 2) call this method with the name of the field
	# e.g. : $cat->save('category') // saves only 'category' field in $cat instance
	# Param :
	# Returns :
	public function save($field = "")
	{
		// Save all fields
		if(empty($field))
		{
			//$data['id'] = '';
			$data['category'] = $this->getCategory();
			$db = new Database();
			return $db->update("categories",$data);
		}
		// Save one field
		else
		{
			$getField = 'get'.ucfirst($field);
			$val = $this->$getField();
			$data[$field] = $val;
			$db = new Database();
			return $db->update("categories",$data,"id='".$this->getId()."'");
		}
	}
	
	##########################
	# Prints the value of the given variable
	# Param : the name of one of the class variables (e.g. 'title' or 'body')
	# Returns :
	public function show($element)
	{
		switch($element)
		{
			case 'category':	echo $this->getCategory(); break;
			default: echo ""; break;
		}
	}

	
	##########################
	# Setters
	
	public function setCategory($category)
	{
		$this->category = $category;
	}
	
	##########################
	# Getters
	
	public function getId()
	{
		return $this->id;
	}
	
	public function getCategory()
	{
		return $this->category;
	}

}

?>